package com.fit.pages;

import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Log;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.fit.annotations.AdminAccess;
import com.fit.annotations.AnonymousAccess;
import com.fit.annotations.CoachAccess;
import com.fit.annotations.MemberAccess;
import com.fit.pages.Index;
import com.fit.security.AuthenticationException;
import com.fit.services.Authenticator;

@AnonymousAccess
@AdminAccess
@MemberAccess
@CoachAccess
public class Login {

	@Property
	private String username;

	@Property
	private String password;

	@Inject
	private Authenticator authenticator;

	@Component
	private Form loginForm;

	@Inject
	private Messages messages;

	@Log
	public Object onSubmitFromLoginForm() {
		try {
			authenticator.login(username, password);
		} catch (AuthenticationException ex) {
			loginForm.recordError(messages.get("error.login"));
			return null;
		}

		return Index.class;
	}
}
